package com.atguigu.srb.core.controller;


import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-05-26
 */
@RestController
@Api(tags = "积分等级管理模块")
@RequestMapping("/integralGrade")
public class IntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;
    //1、查询所有
    @GetMapping
    public List<IntegralGrade> list(){
        return integralGradeService.list();
    }
    //2、分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("page/{pageNum}/{pageSize}")
    public IPage<IntegralGrade> page(@ApiParam(value = "页码",example = "1") @PathVariable("pageNum") Integer pageNum,
                                     @ApiParam(value = "每页记录数",example = "2")@PathVariable("pageSize") Integer pageSize,
                                     @ApiParam(value = "条件",example = "10")Integer key){//查询积分等级 起始积分超过key的分页数据
        IPage<IntegralGrade> page = new Page<>(pageNum,pageSize);
//        integralGradeService.page(page);
        integralGradeService.queryIntegralGradesByKey(page,key);
        return page;
    }
    //3、新增积分等级
    //mp可以在新增或者更新时 让mybatis的拦截器拦截请求 设置时间值到对象中  因为每个表数据新增更新时 都要操作相同属性 设置相同的值
    @PostMapping
    public boolean save(@RequestBody IntegralGrade integralGrade){
//        integralGrade.setId(); id后端通过mp的雪花算法自动生成
//        integralGrade.setCreateTime(); 后端设置  mp的自动填充  统一配置
//        integralGrade.setUpdateTime();
//        integralGrade.setCreateTime(new Date());
//        integralGrade.setUpdateTime(new Date());
        return integralGradeService.save(integralGrade);
    }
    //4、删除积分等级
    @DeleteMapping("{id}")
    public boolean removeById(@PathVariable("id")Long id){
        return integralGradeService.removeById(id);
    }
    //6、根据id查询积分等级
    @GetMapping("{id}")
    public IntegralGrade getById(@PathVariable("id")Long id){
        return integralGradeService.getById(id);
    }
    //5、更新积分等级
    @PutMapping
    public boolean updateById(@RequestBody IntegralGrade integralGrade){
        return integralGradeService.updateById(integralGrade);
    }

}

